﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System.Text;
using System.Data;
using System.Collections;
using Newtonsoft.Json;

namespace LeSoft.Application.Web.Areas.QualificationManage.Controllers
{
    public class SearchRoomController : MvcControllerBase
    {
        //
        // GET: /QualificationManage/SearchRoom/

        private SearchRoomBillBLL SRBbll = new SearchRoomBillBLL();
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "看房首页", "Index")]
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "看房详情", "Form")]
        public ActionResult Form()
        {
            ViewBag.Man = OperatorProvider.Provider.Current().UserName;
            ViewBag.ManId = OperatorProvider.Provider.Current().UserId;
            ViewBag.Phone = new WorkerBLL().GetEntity(OperatorProvider.Provider.Current().UserId) == null ? "" : new WorkerBLL().GetEntity(OperatorProvider.Provider.Current().UserId).PhoneNum;
            return View();
        }
        #endregion


        #region 获取数据


        /// <summary>
        /// 获取单据状态树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.GetList, "单据状态树", "GetBillStateTreeJson")]
        public ActionResult GetBillStateTreeJson()
        {
            //var data = SRBbll.GetCountList().ToJson().ToJObject();
            List<TreeStatusEntity> lst = new List<TreeStatusEntity>();
            lst.Add(new TreeStatusEntity("0", EnumAttribute.GetDescription(SRStateEnum.All), SRStateEnum.All.ToString(), 0));
            lst.Add(new TreeStatusEntity(SRStateEnum.All.ToString(), EnumAttribute.GetDescription(SRStateEnum.WaitingCheck), SRStateEnum.WaitingCheck.ToString(), 0));
            lst.Add(new TreeStatusEntity(SRStateEnum.All.ToString(), EnumAttribute.GetDescription(SRStateEnum.CheckPassed), SRStateEnum.CheckPassed.ToString(), 0));
            lst.Add(new TreeStatusEntity(SRStateEnum.All.ToString(), EnumAttribute.GetDescription(SRStateEnum.CheckRejected), SRStateEnum.CheckRejected.ToString(), 0));
            lst.Add(new TreeStatusEntity(SRStateEnum.CheckPassed.ToString(), EnumAttribute.GetDescription(SRStateEnum.WaitingSearch), SRStateEnum.WaitingSearch.ToString(), 0));
            lst.Add(new TreeStatusEntity(SRStateEnum.CheckPassed.ToString(), EnumAttribute.GetDescription(SRStateEnum.Searched), SRStateEnum.Searched.ToString(), 0));
            lst.Add(new TreeStatusEntity(SRStateEnum.CheckPassed.ToString(), EnumAttribute.GetDescription(SRStateEnum.Canceled), SRStateEnum.Canceled.ToString(), 0));
            List<TreeEntity> treeList = new List<TreeEntity>();
            foreach (TreeStatusEntity item in lst)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = lst.Count(t => t.PValue == item.Value) == 0 ? false : true;
                tree.id = item.Value;
                tree.text = item.Text;
                tree.value = item.Value;
                if (item.PValue == "0") tree.isexpand = true;
                else tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = item.PValue;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());

        }

        /// <summary>
        /// 获取单据详细信息
        /// </summary>
        /// <param name="BillCode">单据编号or第三方编号</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.GetList, "获取单据详细信息", "GetBillInfo")]
        public ActionResult GetBillInfo(string BillCode)
        {

            var data = SRBbll.GetBillInfo(BillCode);
            return Content(data);

        }

        /// <summary>
        /// 分页获取获取单据列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">查询参数JSon:State、StartTime 、EndTime</param>
        /// <returns></returns>
        ///  
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.GetList, "数据列表", "GetPagList")]
        public ActionResult GetPagList(Pagination pagination, string queryJson)
        {
            if (!string.IsNullOrEmpty(queryJson))
            {
                var queryParam = queryJson.ToJObject();
                if (!queryParam["OrganizeId"].IsEmpty())
                {
                    string OrganizeId = new Cache.PStructCache().GetSonOrganizeId(queryParam["OrganizeId"].ToString());
                    queryParam["OrganizeId"] = OrganizeId;
                } 
                queryJson = queryParam.ToJson();
            }
            var data = SRBbll.GetPageList(pagination, queryJson);
            var watch = CommonHelper.TimerStart();
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());

        }

        #endregion


        #region 保存数据

        /// <summary>
        /// 保存（提交）申请单
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "保存（提交）申请单", "SaveForm")]
        public ActionResult SaveForm(string keyValue, SearchRoomBillEntity entity)
        {

            try
            {
                string BillCode = SRBbll.SaveForm(keyValue, entity);
                return Success("提交成功！");
            }
            catch (Exception)
            {

                return Error("操作失败！");
            }
        }


        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns> 
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Delete, "删除实体", "RemoveForm")]
        public ActionResult RemoveForm(string keyValue)
        {
            bool ok = SRBbll.RemoveForm(keyValue);
            if (ok)
            {
                return Success("删除成功！");
            }
            else
            {
                return Error("删除失败！");
            }
        }
        #endregion

    }
}
